package com.atom.module.document.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.ViewGroup;

/**
 * All rights Reserved, Designed By www.rongdasoft.com
 *
 * @version V1.0
 * @Title: FocusCircleView
 * @Description:
 * @author: wuyongzheng
 * @date: 2022/3/23
 * @Copyright: 2022/3/23 www.rongdasoft.com Inc. All rights reserved.
 */
public class FocusView extends ViewGroup {
    public FocusView(Context context) {
        super(context);
        init();
    }

    public FocusView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FocusView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public FocusView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private final float CIRCLE_RADIUS = 50f;
    private final long CIRCLE_DURATION = 500L;

    private boolean mDrawCircle = false;
    private Handler mHandler;
    private Paint mPaint;
    private float mLastCenterX = 0f;
    private float mLastCenterY = 0f;

    private void init() {
        setWillNotDraw(false);
        mHandler = new Handler();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(2f);
    }

    public void drawFocusCircle(Float x, Float y) {
        mLastCenterX = x;
        mLastCenterY = y;
        toggleCircle(true);
        mHandler.removeCallbacksAndMessages(null);
        mHandler.postDelayed(() -> toggleCircle(false), CIRCLE_DURATION);
    }

    private void toggleCircle(Boolean show) {
        mDrawCircle = show;
        invalidate();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mDrawCircle) {
            canvas.drawCircle(mLastCenterX, mLastCenterY, CIRCLE_RADIUS, mPaint);
        }
    }
}
